<%@ page import="java.util.List" %>
<%@ page import="com.just.just_bankreputationsystem.Model.Page" %>
<%@ page import="com.just.just_bankreputationsystem.Model.Bank" %>

<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: wujiaming
  Date: 2020-11-20
  Time: 15:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>企业列表</title>

    <link rel="stylesheet" href="${pageContext.request.contextPath}/node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/public.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/fanye.css">

    <script>
        var a =1;
        function Bank(id, name, profit, profitRate, reputation,appointment,tid, type) {
            this.id = id;
            this.name = name;
            this.profit = profit;
            this.profitRate = profitRate;
            this.reputation = reputation;
            this.appointment = appointment;
            this.tid = tid;
            this.type = type;
        }

        //创建数组来存储数据
        var myList = new Array();

        <%
            Page myPage = (Page) request.getAttribute("myPage");
            System.out.println(myPage);
            System.out.println(myPage.getContent().get(0).getType().getName());
            //获取总共的数据量
            int totalsize = myPage.getContent().size();
            //包含的数据
            List<Bank> myList = myPage.getContent();
            System.out.println(myList.get(0).getType().getName());
            //获取页数
            int pageNum = myPage.getPageNum();
            //获取页大小
            int pageSize = myPage.getPageSize();
        %>

        //存储总页数
        var PAGENUM =
        <%=pageNum%>
        var PAGESIZE =
        <%=pageSize%>
        var TOTALSIZE = <%=totalsize%>
            <%
                for(int i = 0; i < totalsize; i++) {
             %>
            myList.push(new Bank('<%=myList.get(i).getId()%>', '<%=myList.get(i).getName()%>', '<%=myList.get(i).getProfit()%>', '<%=myList.get(i).getProfitRate()%>', '<%=myList.get(i).getReputation()%>', '<%=myList.get(i).getAppointment()%>','<%=myList.get(i).getTid()%>','<%=myList.get(i).getType().getName()%>'));

        <%
           }
       %>

    //当前页数
        var correctPageNum = 1;

        /**
         * 封装创建表格行的函数
         * 主要使用JS对表格的操作
         */
        function createtablerow() {
            var rows;
            //若是当前页码与总页码不同则显示PAGESIZE行
            if (correctPageNum != PAGENUM)
                rows = PAGESIZE;
            else
            //若是当前页码为最后一页则显示剩余行
                rows = PAGESIZE * (PAGENUM) - TOTALSIZE;
            //创建行函数
            for (var i = 0; i < rows; i++) {
                var table = document.getElementById("tables");
                var newRow = table.insertRow(i+1);
                newRow.setAttribute("class","mytr");

                var cell_id = newRow.insertCell();
                cell_id.innerText = myList[(correctPageNum - 1) * PAGESIZE + i].id;

                var cell_name = newRow.insertCell();
                cell_name.innerText = myList[(correctPageNum - 1) * PAGESIZE + i].name;

                var cell_profit = newRow.insertCell();
                cell_profit.innerText = myList[(correctPageNum - 1) * PAGESIZE + i].profit;

                var cell_profitRate = newRow.insertCell();
                cell_profitRate.innerText = myList[(correctPageNum - 1) * PAGESIZE + i].profitRate;

                var cell_reputation = newRow.insertCell();
                cell_reputation.innerText = myList[(correctPageNum - 1) * PAGESIZE + i].reputation;

                var cell_appointment = newRow.insertCell();
                cell_appointment.innerText = myList[(correctPageNum - 1) * PAGESIZE + i].appointment;

                var cell_type = newRow.insertCell();
                cell_type.innerText = myList[(correctPageNum - 1) * PAGESIZE + i].type;

                 var cell_edit = newRow.insertCell();
                 var targert_id = myList[(correctPageNum - 1) * PAGESIZE + i].id;
                 cell_edit.innerHTML = '<a href="${pageContext.request.contextPath}/BankReputation/edit?id=' + targert_id +'">编辑</a>';

                 var cell_del = newRow.insertCell();
                cell_del.innerHTML = '<a href="${pageContext.request.contextPath}/BankReputation/del?id=' + targert_id + '"'+ 'onclick="return'+"(confirm('确定删除？'))"+'">删除</a>';
            }
        }

        function test() {
            var context = document.getElementById("tables");
            //context.appendChild(maincontext);
            var newRow = context.insertRow(0);
            var cell1 = newRow.insertCell();
            cell1.innerText="aaa";
        }

        function firstpage() {
            correctPageNum = 1;
            var trs = document.getElementsByClassName("mytr");
            if (trs != null) {
                for (var i = 0; i < trs.length; i++) {
                    trs[i].parentNode.removeChild(trs[i]);
                    i--;
                }
            }

            createtablerow();
        }

        function pageup() {
            if (correctPageNum > 1)
                correctPageNum--;

            var trs = document.getElementsByClassName("mytr");
            if (trs != null) {
                for (var i = 0; i < trs.length; i++) {
                    trs[i].parentNode.removeChild(trs[i]);
                    i--;
                }
            }

            createtablerow();

            //console.log(correctPageNum);
        }

        function pagedown() {
            if (correctPageNum < PAGENUM)
                correctPageNum++;

            var trs = document.getElementsByClassName("mytr");
            if (trs != null) {
                for (var i = 0; i < trs.length; i++) {
                    trs[i].parentNode.removeChild(trs[i]);
                    i--;
                }
            }

            createtablerow();

            //console.log(correctPageNum);
        }

        function lastpage() {
            correctPageNum = PAGENUM;

            var trs = document.getElementsByClassName("mytr");
            if (trs != null) {
                for (var i = 0; i < trs.length; i++) {
                    trs[i].parentNode.removeChild(trs[i]);
                    i--;
                }
            }

            createtablerow();
        }
        function goToLocation() {
            var str = document.getElementById("inputText").value;
            var tableId = document.getElementById("tables");
            var test = 1;
            for (var i = 0; i < tableId.rows.length; i++) {
                if (str == tableId.rows[i].cells[1].innerHTML) {
                    test = 0;
                    tableId.rows[i].style.background = "aquamarine";
                    // alert("您查找的企业存在！");
                    break;
                }
            }
            if (test == 1) {
                alert("您查找的企业不存在,请您添加该企业！");
            }
        }
</script>

</head>
<div class="container-fluid" style="background-color:#88ccffcf;">
    <div class="row">
        <div class="col-md-12">
            <nav class="navbar navbar-expand-lg publicHeader" style="background-color:#88ccffcf;">
                <div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">
                    <ul class="navbar-nav" id="publicUl">
                        <li class="nav-item active">
                            <h3>企业信誉评定系统</h3>
                        </li>
                        <li class="nav-item active " id="ban_li">
                            <a class="nav-link" href="toAdd">添加企业</a>
                        </li>
                    </ul>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <form class="form-inline">
                        <input id="inputText" class="form-control mr-sm-2" type="text"/>
                        <button class="btn btn-primary my-2 my-sm-0" type="button" onclick="goToLocation()">
                            Search
                        </button>
                    </form>

                    <ul class="navbar-nav ml-md-auto">
                        <div class="publicHeaderR" style="position:relative;top:10px;">
                            <p><span id="hours">下午好！</span><span style="color: #fff21b">${user.name}</span> , 欢迎你！</p>
                            <a href="${pageContext.request.contextPath}/user/login">退出</a>
                        </div>
                    </ul>
                </div>
            </nav>
        </div>
    </div>
</div>

<section class="publicTime">
    <span id="time" style="font-size: 18px;">2020年12月01日 0:02  星期二</span>
</section>


<div class="left" style="float:left">

        <ul class="list">
            <li><a href="" style="font-size:18px;">功能列表</a></li>
            <li ><a href="${pageContext.request.contextPath}/BankReputation/list">企业管理</a></li>
            <li><a href="${pageContext.request.contextPath}/Type/list">类型管理</a></li>
            <li><a href="#">用户管理</a></li>
            <li><a href="#">数据分析</a></li>
            <li><a href="${pageContext.request.contextPath}/user/login">退出系统</a></li>
        </ul>
</div>

     <div class="row" style="width: 1250px; float:left;margin-left:50px;">
        <div class="col-md-12">
            <table id="tables" class="table table-bordered table-sm table-hover">
                 <thead>
                    <tr id="tablehead" >
                        <th>
                            银行ID
                        </th>
                        <th>
                            名称
                        </th>
                        <th>
                            利润
                        </th>
                        <th>
                            利润率
                        </th>
                        <th>
                            信誉
                        </th>
                        <th>
                            是否违约
                        </th>
                        <th>
                            企业类型
                        </th>
                        <th>
                            操作
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                </thead>

                <script>firstpage()</script>

            </table>
        </div>

         <div class="row" id="location">
                <div class="col-md-12">
                    <ul id="fanye_left">
                        <li><button type="button" onclick="firstpage()">首页</button></li>
                        <li><button type="button" onclick="pageup()">上一页</button></li>
                    </ul>
                    <ul id="fanye_right">
                        <li><button type="button" onclick="pagedown()">下一页</button></li>
                        <li><button type="button" onclick="lastpage()">尾页</button></li>
                    </ul>
                </div>
         </div>

    </div>



<%--<script src="node_modules/jquery/dist/jquery.slim.js"></script>--%>
<script src="${pageContext.request.contextPath}/node_modules/jquery/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="${pageContext.request.contextPath}/js/time.js"></script>


<script>

</script>
</body>
</html>
